﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using E2_WEBSITE.Code.Product;
using E2_WEBSITE.Code;

public partial class _Default : System.Web.UI.Page 
{
    private ProductController m_ProductController = new ProductController();

    protected void Page_Load(object sender, EventArgs e)
    {        
        Page.LoadComplete += new EventHandler(Page_LoadComplete);
    }
        
    void Page_LoadComplete(object sender, EventArgs e)
    {
        //fill table "San pham moi tren thi truong"
        List<ProductInfo> lst = m_ProductController.getTopNewProduct(6);
        if (lst == null)
            return;

        string sImgBtn = "ImgBtnSPMoi";
        string sHLink = "HLinkSPMoi";
        string sLable = "lSPMoi";

        int i = 1;
        for (i = 0; i < lst.Count; i++)
        {
            string sNameControl = string.Empty;
            sNameControl = sImgBtn + (i+1).ToString();
            ImageButton imgBtn = (ImageButton)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (imgBtn != null)
            {
                imgBtn.ImageUrl = lst[i].Hinh_Anh.URL;
                imgBtn.PostBackUrl = "~//Products//detail.aspx?ProductId=" + lst[i].ID.ToString();
            }

            sNameControl = string.Empty;
            sNameControl = sHLink + (i+1).ToString();
            HyperLink hLink = (HyperLink)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (hLink != null)
            {
                hLink.NavigateUrl = "~//Products//detail.aspx?ProductId=" + lst[i].ID.ToString();
                hLink.Text = lst[i].TenSanPham;
            }

            sNameControl = string.Empty;
            sNameControl = sLable + (i+1).ToString();
            Label label = (Label)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (label != null)
            {
                label.Text = Utilities.ConvertToMoneyFormat(lst[i].Gia) + " VNĐ";                
            }
        }        
                
        //fill table "San pham duoc yeu thich"
        lst.Clear();
        lst = m_ProductController.getTopFavoriteProduct(6);
        if (lst == null)
            return;

        sImgBtn = "ImgBtnSPYeuThich";
        sHLink = "HLinkSPYeuThich";
        sLable = "lSPYeuThich";
                
        
        for (i = 0; i < lst.Count; i++)
        {
            string sNameControl = string.Empty;
            sNameControl = sImgBtn + (i+1).ToString();
            ImageButton imgBtn = (ImageButton)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (imgBtn != null)
            {
                imgBtn.ImageUrl = lst[i].Hinh_Anh.URL;
                imgBtn.PostBackUrl = "~//Products//detail.aspx?ProductId=" + lst[i].ID.ToString();
            }

            sNameControl = string.Empty;
            sNameControl = sHLink + (i+1).ToString();
            HyperLink hLink = (HyperLink)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (hLink != null)
            {
                hLink.NavigateUrl = "~//Products//detail.aspx?ProductId=" + lst[i].ID.ToString();
                hLink.Text = lst[i].TenSanPham;
            }

            sNameControl = string.Empty;
            sNameControl = sLable + (i+1).ToString();
            Label label = (Label)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (label != null)
                label.Text = Utilities.ConvertToMoneyFormat(lst[i].Gia) + " VNĐ";

        }


        //fill table "San pham ban chay nhat"
        lst.Clear();
        lst = m_ProductController.getTopBestSellProduct(6);
        if (lst == null)
            return;

        sImgBtn = "ImgBtnSPBanChay";
        sHLink = "HLinkSPBanChay";
        sLable = "lSPMoiBanChay";

        for (i = 0; i < lst.Count; i++)
        {
            string sNameControl = string.Empty;
            sNameControl = sImgBtn + (i + 1).ToString();
            ImageButton imgBtn = (ImageButton)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (imgBtn != null)
            {
                imgBtn.ImageUrl = lst[i].Hinh_Anh.URL;
                imgBtn.PostBackUrl = "~//Products//detail.aspx?ProductId=" + lst[i].ID.ToString();
            }

            sNameControl = string.Empty;
            sNameControl = sHLink + (i + 1).ToString();
            HyperLink hLink = (HyperLink)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (hLink != null)
            {
                hLink.NavigateUrl = "~//Products//detail.aspx?ProductId=" + lst[i].ID.ToString();
                hLink.Text = lst[i].TenSanPham;
            }

            sNameControl = string.Empty;
            sNameControl = sLable + (i + 1).ToString();
            Label label = (Label)this.Master.FindControl("ContentPlaceHolder1").FindControl(sNameControl);
            if (label != null)
                label.Text = Utilities.ConvertToMoneyFormat(lst[i].Gia) + " VNĐ";

        }  
                
    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
}
